<!--
###############
Author: Darren Stather
Contact: darrenstather1@gmail.com
Version: 1.7.5

Copyright (C) 2014  Darren Stather

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
###############
-->
<html>
<head>
<title>AsseTools</title>
<HTA:APPLICATION 
	APPLICATIONNAME="AsseTools"
	SCROLL="no"
	SINGLEINSTANCE="yes"
	MAXIMIZEBUTTON="no"
	MINIMIZEBUTTON="no"
>
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<style type="text/css">
body, .headerItem, .inputTitle, textarea, .optiontext, .button, select, .text, #divStatus { font-family: Arial; font-size: 12px; }
body { background-color: #eeeeee; margin: 10px; }
.NoBorder { border: 0; }

#divHeader, #divFooter, #divOptions, .container { background-color: #FFFFFF; box-shadow: 0 2px 0 #dddddd; border-radius: 2px; content: '';  margin-bottom: 10px; }
#divHeader { height: 35px; width: 360px; }
#divFooter { height: 150px; width: auto; padding: 10px; }
#divOptions { height: 38px; width: 522px; padding: 10px; }
.headerItem { width: 120px; float: left; line-height: 35px; text-align: center; vertical-align: middle; }
.optionsItem { float: left; }

.container { width: auto; height: 250px; padding: 10px; }
.inputTitle { vertical-align: top; margin-bottom: 5px; }
.inputBody { height: 200px; }
.inputBodyOneSelect { height: 171px; }
.inputBodyTwoSelect { height: 142px; }
textarea { height: 100%; width: 100%; border: 1px solid #DDDDDD; box-shadow: inset 0px 0px 2px #DDDDDD; border-radius: 2px; overflow: auto; padding: 2px; font-family: tahoma; font-size: 11px;}
.optiontext, .text { vertical-align: bottom; height: 18px; border: 1px solid #DDDDDD; box-shadow: inset 0px 0px 2px #DDDDDD; padding-left: 2px; border-radius: 2px; font-size: 12px; }
.optiontext { width: 150px; }
.text { width: 100%; }

#divDomainCon { margin: 0 50px 0 0; }
#divUsernameCon { margin: 0 5px 0 0; }

#divLog { width: 99.5%; height: 100px; }
.button { height: 25px; background-color: #f4f4f4; border: 1px solid #cccccc; border-radius: 2px; font-weight: bold; cursor: hand; }

#divComputersInputCon, #divUsersInputCon { width: 25%; margin-right: 10px; float: left; }
#divComputersOutputCon, #divUsersOutputCon { margin-right: 10px; }
#divComputersOutputCon, #divComputersResultsCon, #divUsersOutputCon, #divUsersResultsCon { width: 35.9%; float: left; }

#divConnectionsResultsCon { width: 35.9%; float: left; }
#divConnectionsComputerCon, #divConnectionsUserCon { width: 30.45%; float: left; margin-right: 10px; }

#divNavAll { width: 100%; height: 25px; margin-top: 10px; clear: both; float: left; }
#divComputersNav, #divUsersNav, #divConnectionsNav { width: 100%; height: 25px; margin-top: 4px; clear: both; float: left; }
#divConnectionsNav {  margin-top: 10px; }
#divNavAllLeft, #divComputersNavLeft, #divUsersNavLeft, #divConnectionsNavLeft { float: left; }
#divNavAllRight, #divComputersNavRight, #divUsersNavRight, #divConnectionsNavRight { float: right; }

#ddComputersInputWant, #ddUsersInputHave, #ddUsersOutputGroups { height: 25px; width: 103%; border: 1px solid #DDDDDD; box-shadow: inset 0px 0px 2px #DDDDDD; border-radius: 2px; margin-top: 10px; }
#ddUsersInputWant, #ddConnectionsWant, #ddConnectionsUser { height: 25px; width: 103%; border: 1px solid #DDDDDD; box-shadow: inset 0px 0px 2px #DDDDDD; border-radius: 2px; margin-top: 4px; }

#divStatus { position: absolute; top: 2px; right: 2px; content: ''; width: 200px; height: 50px; z-index: 1; color: #FF0000; font-weight: bold; text-align: right;}

.a_black { color: #000000; }

</style>

<body>

<div id="divStatus"></div>

<div id="divOptions">
	<div id="divDomainCon" class="optionsItem">
		<div class="inputTitle">Domain</div>
		<input type="text" class="optiontext" id="tiDomain">
	</div>
	<div id="divUsernameCon" class="optionsItem">
		<div class="inputTitle">Username</div>
		<input type="text" class="optiontext" id="tiUsername">
	</div>
	<div id="divPasswordCon" class="optionsItem">
		<div class="inputTitle">Password</div>
		<input type="password" class="optiontext" id="tiPassword">
	</div>
</div>

<div id="divHeader">
	<a href="#" class="a_black"><div id="divHeaderComputers" class="headerItem" style="border-bottom: solid 2px #529AEA; font-weight: bold;" onclick="jsType('computers')">
		Computers
	</div></a>
	<a href="#" class="a_black"><div id="divHeaderUsers" class="headerItem" onclick="jsType('users')">
		Users
	</div></a>
	<a href="#" class="a_black"><div id="divHeaderConnections" class="headerItem" onclick="jsType('connections')">
		Connections
	</div></a>
</div>

<div id="divComputers" class="container" style="display: ;">
	<div id="divComputersInputCon">
		<div class="inputTitle">Input</div>
		<div id="divComputersInput" class="inputBodyOneSelect">
			<textarea id="tiComputersInput" name="tiComputersInput"></textarea>
		</div>
		<select id="ddComputersInputWant">
			<option selected value="I Want">I Want</option>
			<option value="Full Asset Number">- Full Asset Number</option>
			<option value="Full Asset Number and...">Full Asset Number and...</option>
			<option value="description">- Description</option>
			<option value="distinguishedName">- Distinguished Name</option>
			<option value="operatingSystem">- Operating System</option>
			<option value="operatingSystemServicePack">- Service Pack</option>
			<option value="objectSid">- SID</option>
		</select>
	</div>
	<div id="divComputersOutputCon">
		<div class="inputTitle">Output</div>
		<div id="divComputersOutput" class="inputBody">
			<textarea id="tiComputersOutput" name="tiComputersOutput"></textarea>
		</div>
	</div>
	<div id="divComputersResultsCon">
		<div class="inputTitle">Results</div>
		<div id="divComputersResults" class="inputBody">
			<textarea id="tiComputersResults" name="tiComputersResults" readonly></textarea>
		</div>
	</div>
	<div id="divComputersNav">
		<div id="divComputersNavLeft">
			<input type="button" value="Go" class="button" onclick="jsScriptRun('get_computers')">
			<input type="button" value="Ping" class="button" onclick="jsScriptRun('ping')">
			<input type="button" value="Get IP" class="button" onclick="jsScriptRun('get_ip')">
			<input type="button" value="Get Apps" class="button" onclick="jsScriptRun('apps')">
			<input type="checkbox" id="ckComputersNoMatch" class="NoBorder">
			<label for="ckComputersNoMatch">Skip No Matches</label>
		</div>
		<div id="divComputersNavRight">
			<input type="button" value="Reset" class="button" onclick="jsReset('computers')">
		</div>
	</div>
</div>

<div id="divUsers" class="container" style="display: none;">
	<div id="divUsersInputCon">
		<div class="inputTitle">Input</div>
		<div id="divUsersInput" class="inputBodyTwoSelect">
			<textarea id="tiUsersInput" name="tiUsersInput"></textarea>
		</div>
		<select id="ddUsersInputHave">
			<option selected value="I Have">I Have</option>
			<option value="sAMAccountName">- Account Name</option>
			<option value="displayName">- Display Name</option>
			<option value="distinguishedName">- Distinguished Name</option>
			<option value="givenName">- First Name</option>
			<option value="mail">- Email Address</option>
			<option value="mobile">- Mobile Number</option>
			<option value="sn">- Surname</option>
			<option value="telephoneNumber">- Telephone Number</option>
			<option value="objectSid">- SID</option>
		</select>
		<select id="ddUsersInputWant">
			<option selected value="I Want">I Want</option>
			<option value="sAMAccountName">- Account Name</option>
			<option value="Account Name and...">Account Name and...</option>
			<option value="displayName">- Display Name</option>
			<option value="givenName">- First Name</option>
			<option value="sn">- Surname</option>
			<option value="mail">- Email Address</option>
			<option value="telephoneNumber">- Telephone Number</option>
			<option value="mobile">- Mobile Number</option>
			<option value="Group Membership">- Group Membership</option>
			<option value="distinguishedName">- Distinguished Name</option>
			<option value="objectSid">- SID</option>
		</select>
	</div>
	<div id="divUsersOutputCon">
		<div class="inputTitle">Output</div>
		<div id="divUsersOutput" class="inputBodyOneSelect">
			<textarea id="tiUsersOutput" name="tiUsersOutput"></textarea>
		</div>
		<select id="ddUsersOutputGroups"></select>
	</div>
	<div id="divUsersResultsCon">
		<div class="inputTitle">Results</div>
		<div id="divUsersResults" class="inputBody">
			<textarea id="tiUsersResults" name="tiUsersResults" readonly></textarea>
		</div>
	</div>
	<div id="divUsersNav">
		<div id="divUsersNavLeft">
			<input type="button" value="Go" class="button" onclick="jsScriptRun('get_users')">
			<input type="button" value="Group Members" class="button" onclick="jsScriptRun('group_members')">
		</div>
		<div id="divUsersNavRight">
			<input type="button" value="Reset" class="button" onclick="jsReset('users')">
		</div>
	</div>
</div>

<div id="divConnections" class="container" style="display: none;">
	<div id="divConnectionsComputerCon">
		<div class="inputTitle">Computer</div>
		<div id="divConnectionsComputer" class="inputBody">
			<input type="text" id="tiConnectionsComputer" name="tiConnectionsComputer" class="text">
			<select id="ddConnectionsWant">
				<option selected value="I Want">I Want</option>
				<option value="Network Drives">- Network Drives</option>
				<option value="Printers">- Printers</option>
			</select>
		</div>
	</div>
	<div id="divConnectionsUserCon">
		<div class="inputTitle">User</div>
		<div id="divConnectionsUser" class="inputBody">
			<input type="text" id="tiConnectionsUser" name="tiConnectionsUser" class="text">
			<select id="ddConnectionsUser" onchange="jsConnectionsUser()"></select>
			<div id="divConnectionsProfileLocation"></div>
			<div id="divConnectionsProfileSize"></div>
		</div>
	</div>
	<div id="divConnectionsResultsCon">
		<div class="inputTitle">Results</div>
		<div id="divConnectionsResults" class="inputBody">
			<textarea id="tiConnectionsResults" name="tiConnectionsResults" readonly></textarea>
		</div>
	</div>
	<div id="divConnectionsNav">
		<div id="divConnectionsNavLeft">
			<input type="button" value="Go" class="button" onclick="jsScriptRun('connections')">
			<input type="button" value="Get Users" class="button" onclick="jsScriptRun('list_users')">
			<input type="button" value="Profile Details" class="button" onclick="jsScriptRun('profile_details')">
		</div>
		<div id="divConnectionsNavRight">
			<input type="button" value="Reset" class="button" onclick="jsReset('connections')">
		</div>
	</div>
</div>

<div id="divFooter">
	<div class="inputTitle">Log</div>
	<div id="divLog">
		<textarea id="tiLog" name="tiLog"></textarea>
	</div>
	<div id="divNavAll">
		<div id="divNavAllLeft">
			<input type="button" value="Clear Log" class="button" onclick="jsClearLog()">
			<input type="button" value="Full Reset" class="button" onclick="jsFullReset()">
		</div>
		<div id="divNavAllRight">
			<input type="button" id="btExit" value="Exit" class="button" onclick="jsScriptRun('exit')">
		</div>
	</div>
</div>

</body>

<script language="vbscript" type="text/vbscript">

Dim globHostname
Dim globOS
globOS = 0
vbStartUp()

Function vbStartUp()

	'width, height
	Window.resizeTo 850,624
	
	Dim objWMISvc, colItems, strComputerDomain, oss, strOS

	Set objWMISvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	Set colItems = objWMISvc.ExecQuery("Select * from Win32_ComputerSystem")
	For Each objItem in colItems
		strComputerDomain = objItem.Domain
		globHostname = objItem.Name
		If objItem.PartOfDomain Then
			document.all.tiDomain.value = strComputerDomain
		Else
			document.all.tiLog.value = document.all.tiLog.value & "ERROR: Computer isn't part of a Domain."
		End If
	Next
	
	Set oss = objWMISvc.ExecQuery ("Select * from Win32_OperatingSystem")
	For Each os in oss
		If InStr(os.Caption,"Windows XP") > 0 Then
			document.all.tiLog.value = document.all.tiLog.value & "COMPATIBILITY ISSUE: You're using Windows XP. You may experience display issues."
			globOS = 1
		End If
	Next
	
End Function

Function vbGetComputers()
	
	jsUpdateStatus("Please wait...")
	vbSleepy()
	
	Dim vNum, vNum1
	Dim vBody
	Dim objFSO, objFile, objConnection, objCommand, objRecordSet, vData, oShell, vAppData
	Dim vDomain
	Dim vInput
	Dim vAssets
	Dim vAsset
	Dim vWant
	Dim vTotal
	
	vNum = 0
	vNum1 = 0
	vTotal = 0
	
	vDomain = document.all.tiDomain.value
	vWant = document.all.ddComputersInputWant.value
	
	vInput = jsTrim(document.all.tiComputersInput.value)
	If globOS = 1 Then
		vAssets = Split(vInput,vbCrLf)
	Else
		vAssets = Split(vInput,Chr(10))
	End If
	vTotal = UBound(vAssets) + 1
	
	document.all.tiComputersOutput.value = ""
	
	Set objConnection = CreateObject("ADODB.Connection")
	Set objCommand = CreateObject("ADODB.Command")
	objConnection.Provider = "ADsDSOObject"
	objConnection.Open "Active Directory Provider"

	Set objCommand.ActiveConnection = objConnection

	If vDomain <> "" Then
	
		For Each vAsset in vAssets
		
			vNum = vNum + 1
			If vWant = "Full Asset Number" Then
				If Left(vAsset,1) = "=" Then
					objCommand.CommandText = "Select Name from 'LDAP://" & vDomain & "' Where objectClass='computer' And cn='" & Right(vAsset,Len(vAsset)-1) & "'"
				Else
					objCommand.CommandText = "Select Name from 'LDAP://" & vDomain & "' Where objectClass='computer' And cn='*" & vAsset & "*'"
				End If
			Else
				If Left(vAsset,1) = "=" Then
					objCommand.CommandText = "Select Name, " & vWant & " from 'LDAP://" & vDomain & "' Where objectClass='computer' And cn='" & Right(vAsset,Len(vAsset)-1) & "'"
				Else
					objCommand.CommandText = "Select Name, " & vWant & " from 'LDAP://" & vDomain & "' Where objectClass='computer' And cn='*" & vAsset & "*'"
				End If
			End If
			objCommand.Properties("Page Size") = 5000
			objCommand.Properties("Searchscope") = 2
			On Error Resume Next
				Set objRecordSet = objCommand.Execute
			If Err.Number <> 0 Then
				jsUpdateLog("ERROR: Unable to connect to AD.")
				jsUpdateStatus("Complete")
				Err.Clear
				Exit Function
			End If
			
			If objRecordSet.RecordCount = 0 Then
				document.all.tiComputersOutput.value = document.all.tiComputersOutput.value & vAsset & " :: No matches" & vbCrLf
				vbSleepy()
			Else
				objRecordSet.MoveFirst
				Do Until objRecordSet.EOF
					If vWant = "Full Asset Number" Then
						document.all.tiComputersOutput.value = document.all.tiComputersOutput.value & vAsset & " :: " & objRecordSet.Fields("Name").Value & vbCrLf
						vNum1 = vNum1 + 1
						vbSleepy()
					ElseIf vWant = "objectSid" Then
						document.all.tiComputersOutput.value = document.all.tiComputersOutput.value & vAsset & " :: " & objRecordSet.Fields("Name").Value & " :: " & HexSIDToDec(OctetToHexStr(objRecordSet.Fields(vWant).Value)) & vbCrLf
						vNum1 = vNum1 + 1
						vbSleepy()
					Else
						document.all.tiComputersOutput.value = document.all.tiComputersOutput.value & vAsset & " :: " & objRecordSet.Fields("Name").Value & " :: " & objRecordSet.Fields(vWant).Value & vbCrLf
						vNum1 = vNum1 + 1
						vbSleepy()
					End If
					
					objRecordSet.MoveNext
				Loop
				
			End If
			
		Next
		
		If vNum = 1 And vNum1 <> 1 Then
			jsUpdateLog(vNum1 & " computers found from " & vNum & " entry")
		ElseIf vNum <> 1 And vNum1 = 1 Then
			jsUpdateLog(vNum1 & " computer found from " & vNum & " entries")
		ElseIf vNum = 1 And vNum1 = 1 Then
			jsUpdateLog(vNum1 & " computer found from " & vNum & " entry")
		End If
		
	Else
	
		jsUpdateLog("ERROR: No domain.")
	
	End If
	
	jsUpdateStatus("Complete")
	vbSleepy()
	
End Function

Function vbGetUsers()
	
	jsUpdateStatus("Please wait...")
	vbSleepy()
	
	Dim vNum, vNum1
	Dim vBody
	Dim objFSO, objFile, objConnection, objCommand, objRecordSet, vData, oShell, vAppData
	Dim vDomain
	Dim vInput
	Dim vUsers
	Dim vUser
	Dim vType
	Dim vHave, vWant
	Dim vTotal
	vTotal = 0
	
	vDomain = document.all.tiDomain.value
	
	'vInput = jsTrim(document.all.tiUsersInput.value)
	vInput = document.all.tiUsersInput.value
	If globOS = 1 Then
		vUsers = Split(vInput,vbCrLf)
	Else
		vUsers = Split(vInput,Chr(10))
	End If
	vTotal = UBound(vUsers) + 1
	vHave = document.all.ddUsersInputHave.value
	vWant = document.all.ddUsersInputWant.value
	vNum = 0
	vNum1 = 0
	
	Set objConnection = CreateObject("ADODB.Connection")
	Set objCommand = CreateObject("ADODB.Command")
	objConnection.Provider = "ADsDSOObject"
	objConnection.Open "Active Directory Provider"

	Set objCommand.ActiveConnection = objConnection
	
	For Each vUser in vUsers
		
		vNum = vNum + 1
		
		If Left(vUser,1) = "=" Then
			objCommand.CommandText = "Select sAMAccountName, " & vWant & " from 'LDAP://" & vDomain & "' Where objectClass='person' And " & vHave & "='" & Right(vUser,Len(vUser)-1) & "'"
		Else
			objCommand.CommandText = "Select sAMAccountName, " & vWant & " from 'LDAP://" & vDomain & "' Where objectClass='person' And " & vHave & "='*" & vUser & "*'"
		End If
		objCommand.Properties("Page Size") = 5000
		objCommand.Properties("Searchscope") = 2
		On Error Resume Next
			Set objRecordSet = objCommand.Execute
		If Err.Number <> 0 Then
			jsUpdateLog("ERROR: Unable to connect to AD.")
			jsUpdateStatus("Complete")
			Err.Clear
			Exit Function
		End If
		
		If objRecordSet.RecordCount = 0 Then
			document.all.tiUsersOutput.value = document.all.tiUsersOutput.value & vUser & " :: No matches" & vbCrLf
			vbSleepy()
		Else
			objRecordSet.MoveFirst
			Do Until objRecordSet.EOF
				If vWant = "objectSid" Then
					document.all.tiUsersOutput.value = document.all.tiUsersOutput.value & vUser & " :: " & objRecordSet.Fields("sAMAccountName").Value & " :: " & HexSIDToDec(OctetToHexStr(objRecordSet.Fields(vWant).Value)) & vbCrLf
					vNum1 = vNum1 + 1
					vbSleepy()
				Else
					document.all.tiUsersOutput.value = document.all.tiUsersOutput.value & vUser & " :: " & objRecordSet.Fields("sAMAccountName").Value & " :: " & objRecordSet.Fields(vWant).Value & vbCrLf
					vNum1 = vNum1 + 1
					vbSleepy()
				End If
				objRecordSet.MoveNext
			Loop
			
		End If
		
	Next
	
	If vNum = 1 And vNum1 <> 1 Then
		jsUpdateLog(vNum1 & " users found from " & vNum & " entry")
	ElseIf vNum <> 1 And vNum1 = 1 Then
		jsUpdateLog(vNum1 & " user found from " & vNum & " entries")
	ElseIf vNum = 1 And vNum1 = 1 Then
		jsUpdateLog(vNum1 & " user found from " & vNum & " entry")
	Else
		jsUpdateLog(vNum1 & " users found from " & vNum & " entries")
	End If
	
	jsUpdateStatus("Complete")
	vbSleepy()
	
End Function

Function vbSleepy()

    Set objShell = CreateObject("WScript.Shell")
    strCmd = "%COMSPEC% /c"
    objShell.Run strCmd,0,1
	
End Function

Function vbSplit()

	vInit = document.all.tiUsersInput.value
	If globOS = 1 Then
		vSplit = Split(vInit,vbCrLf)
	Else
		vSplit = Split(vInit,Chr(10))
	End If
	For Each vItem in vSplit
	
		MsgBox(vItem)
	
	Next

End Function

Function vbGetIP()

	jsUpdateStatus("Please wait...")
	vbSleepy()
	
	Dim vInput
	Dim vAssets
	Dim vAsset
	Dim objWMIService
	Dim colItems
	Dim objItem
	Dim strIP, strIPSplit, strIPNew
	Dim vPreOutput
	Dim vOutput
	Dim vOutputAll
	Dim vComputer
	Dim vUpdateMess, vUN, vPW
	
	If document.all.tiUsername.value <> "" Then
		vUN = document.all.tiUsername.value
	End If
	If document.all.tiPassword.value <> "" Then
		vPW = document.all.tiPassword.value
	End If
	
	vOutputAll = ""
	
	vInput = jsTrim(document.all.tiComputersOutput.value)
	If globOS = 1 Then
		vAssets = Split(vInput,vbCrLf)
	Else
		vAssets = Split(vInput,Chr(10))
	End If
	
	For Each vAsset in vAssets
			
		If vAsset <> "" Then
		
			vOutput = ""
			
			If InStr(vAsset," :: ") = 0 Then
			
				vComputer = vAsset
			
			Else
			
				vComputer = Mid(vAsset,InStr(vAsset," :: ")+4,Len(vAsset)-InStr(vAsset," :: ")+4)
			
			End If
							
			If document.all.ckComputersNoMatch.checked And vComputer = "No matches" Then
			
				vComputer = "skip"
				
			End If
			
			If vComputer <> "skip" Then
			
				If vbPing(vComputer) = "Ping success" Then
							
					Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
					
					On Error Resume Next
						
						If UCase(vComputer) = UCase(globHostname) Then
						
							Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & vComputer & "\root\cimv2")
						
						Else
						
							If vUN = "" Or vPW = "" Then
								Set objWMIService = objSWbemLocator.ConnectServer(vComputer, "root\cimv2")
							Else
								Set objWMIService = objSWbemLocator.ConnectServer(vComputer, "root\cimv2", vUN, vPW)
							End If
							
						End If
			
						Set colItems = objWMIService.ExecQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='True'")

						For Each objItem In colItems
							strIP = Join(objItem.IPAddress, ",")
							If InStr(strIP,",") > 1 Then
								strIPSplit = Split(strIP,",")
								strIPNew = strIPSplit(0)
							Else
								strIPNew = strIP
							End If
							If strIPNew <> "" Then
								document.all.tiComputersResults.value = document.all.tiComputersResults.value & vComputer & " :: " & strIPNew & vbCrLf
								vUpdateMess = "IP address found for """ & vComputer & """"
								vbSleepy()
							End If
						Next
					
					If Err.Number <> 0 Then
						document.all.tiComputersResults.value = document.all.tiComputersResults.value & vComputer & " :: Ping success, but unable to connect" & vbCrLf
						Err.Clear
						vUpdateMess = "Unable to connect to """ & vComputer & """"
						vbSleepy()
					End If
				
				Else
			
					document.all.tiComputersResults.value = document.all.tiComputersResults.value & vComputer & " :: Ping failed" & vbCrLf
					vUpdateMess = "Failed to ping """ & vComputer & """"
					vbSleepy()
				
				End If
				
			End If
			
			'vOutputAll = vOutputAll & vOutput & vbCrLf
			'document.all.tiComputersResults.value = vOutputAll
			jsUpdateLog(vUpdateMess)
			vbSleepy()
		
		End If
		
	Next
	
	jsUpdateStatus("Complete")
	vbSleepy()
	
End Function

Function vbPingComputers()
	
	jsUpdateStatus("Please wait...")
	vbSleepy()
	
	Dim vInput
	Dim vAssets
	Dim vAsset
	Dim objWMIService
	Dim colItems
	Dim objItem
	Dim strIP, strIPSplit, strIPNew
	Dim vPreOutput
	Dim vOutput
	Dim vOutputAll
	Dim vComputer
	Dim vUpdateMess, vUN, vPW
	Dim vSuccess, vTotal, vFail
	
	vOutputAll = ""
	vTotal = 0
	vSuccess = 0
	vFail = 0
	
	vInput = jsTrim(document.all.tiComputersOutput.value)
	If globOS = 1 Then
		vAssets = Split(vInput,vbCrLf)
	Else
		vAssets = Split(vInput,Chr(10))
	End If
	
	For Each vAsset in vAssets
		
		If vAsset <> "" Then
			
			vTotal = vTotal + 1
			vOutput = ""
			
			If InStr(vAsset," :: ") = 0 Then
			
				vComputer = vAsset
			
			Else
			
				vComputer = Mid(vAsset,InStr(vAsset," :: ")+4,Len(vAsset)-InStr(vAsset," :: ")+4)
			
			End If
			
			If document.all.ckComputersNoMatch.checked And vComputer = "No matches" Then
			
				vComputer = "skip"
				
			End If
			
			If vComputer <> "skip" Then
			
				If vbPing(vComputer) = "Ping success" Then
							
					document.all.tiComputersResults.value = document.all.tiComputersResults.value & vComputer & " :: Ping success" & vbCrLf
					vSuccess = vSuccess + 1
					vbSleepy()
				
				Else

					document.all.tiComputersResults.value = document.all.tiComputersResults.value & vComputer & " :: Ping failed" & vbCrLf
					vFail = vFail + 1
					vbSleepy()
					
				End If
				
			End If
		
		End If
		
	Next
	
	jsUpdateLog("Out of " & vTotal & " computers, " & vSuccess & " pinged successfully, " & vFail & " failed")
	
	jsUpdateStatus("Complete")
	vbSleepy()
	
End Function

Function vbPing(vHost)

	Dim vPing
	Dim vRetStatus

	On Error Resume Next
		Set vPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & vHost & "'")
		For Each vRetStatus In vPing
			If IsNull(vRetStatus.StatusCode) Or vRetStatus.StatusCode <> 0 Then
				vbPing = "Ping failed"
			Else
				vbPing = "Ping success"
			End If
		Next
	If Err.Number <> 0 Then
		vbPing = "Error connecting"
		Err.Clear
	End If

End Function

Function vbGetGroups()
	
	jsUpdateStatus("Please wait...")
	vbSleepy()

	Dim vNum
	Dim vBody
	Dim objFSO, objFile, objConnection, objCommand, objRecordSet, vData, oShell, vAppData
	Dim vDomain
	Dim vInput
	Dim vUsers
	Dim vUser
	Dim x
	Dim vGroups
	Dim vGroupsArr
	Dim y
	Dim vUserGroup
	Dim vUserNum
	Dim vGroupNum
	
	vDomain = document.all.tiDomain.value
	
	vInput = trim(document.all.tiUsersInput.value)
	If globOS = 1 Then
		vUsers = Split(vInput,vbCrLf)
	Else
		vUsers = Split(vInput,Chr(10))
	End If
	vUserNum = 0
	vGroupNum = 0
	
	Set objConnection = CreateObject("ADODB.Connection")
	Set objCommand = CreateObject("ADODB.Command")
	objConnection.Provider = "ADsDSOObject"
	objConnection.Open "Active Directory Provider"

	Set objCommand.ActiveConnection = objConnection
	
	For Each vUser in vUsers
		
		vNum = 0
		
		If Left(vUser,1) = "=" Then
			objCommand.CommandText = "Select ADsPath, sAMAccountName from 'LDAP://" & vDomain & "' Where objectClass='person' And sAMAccountName='" & Right(vUser,Len(vUser)-1) & "'"
		Else
			objCommand.CommandText = "Select ADsPath, sAMAccountName from 'LDAP://" & vDomain & "' Where objectClass='person' And sAMAccountName='*" & vUser & "*'"
		End If
		objCommand.Properties("Page Size") = 5000
		objCommand.Properties("Searchscope") = 2
		On Error Resume Next
			Set objRecordSet = objCommand.Execute
		If Err.Number <> 0 Then
			jsUpdateLog("ERROR: Unable to connect to AD.")
			jsUpdateStatus("Complete")
			Err.Clear
			Exit Function
		End If
		
		If objRecordSet.RecordCount = 0 Then
			document.all.tiUsersOutput.value = document.all.tiUsersOutput.value & vUser & " :: No matches" & vbCrLf
			vbSleepy()
		Else
			objRecordSet.MoveFirst
			Do Until objRecordSet.EOF
				vUserNum = vUserNum + 1
				x = objRecordSet.Fields("ADsPath").Value
				Set objUser = GetObject(x)
				
				If IsArray(objUser.memberOf) = False Then
					
					vGroupNum = vGroupNum + 1
					vUserGroup = Mid(objUser.memberOf,4,InStr(objUser.memberOf,",")-4)
					document.all.tiUsersOutput.value = document.all.tiUsersOutput.value & vUser & " :: " & objRecordSet.Fields("sAMAccountName").Value & " :: " & vUserGroup & vbCrLf
					vbSleepy()
					If InStr(vGroups,vUserGroup) = 0 Then
						vGroups = vGroups & vUserGroup & ","
					End If
					
				Else
					
					If UBound(objUser.memberOf) <> "" Then
						objGroups = objUser.GetEx("memberOf")
						For Each objGroup in objGroups
							vGroupNum = vGroupNum + 1
							vUserGroup = Mid(objGroup,4,InStr(objGroup,",")-4)
							document.all.tiUsersOutput.value = document.all.tiUsersOutput.value & vUser & " :: " & objRecordSet.Fields("sAMAccountName").Value & " :: " & vUserGroup & vbCrLf
							vbSleepy()
							If InStr(vGroups,vUserGroup) = 0 Then
								vGroups = vGroups & vUserGroup & ","
							End If
						Next
					End If
					
				End If
				
				Set objUser = Nothing
				objRecordSet.MoveNext
			Loop
		
		End If
		
	Next
	
	jsRemoveDD("ddUsersOutputGroups")
	vGroupsArr = Split(vGroups,",")
	
	For Each y in vGroupsArr
		If y <> "" Then
			jsAddDDItem y,"ddUsersOutputGroups"
		End If
	Next
	
	dim vUserNumText
	dim vGroupNumText
	If vUserNum = 1 Then
		vUserNumText = "user"
	Else
		vUserNumText = "users"
	End If
	If vGroupNum = 1 Then
		vGroupNumText = "group"
	Else
		vGroupNumText = "groups"
	End If
	
	jsUpdateLog(vGroupNum & " " & vGroupNumText & " found from " & vUserNum & " " & vUserNumText)
	
	jsUpdateStatus("Complete")
	vbSleepy()
	
End Function

Function vbGetMembers()
	
	jsUpdateStatus("Please wait...")
	vbSleepy()

	Dim vNum
	Dim vBody
	Dim objFSO, objFile, objConnection, objCommand, objRecordSet, vData, oShell, vAppData
	Dim vDomain
	Dim vInput
	Dim vUsers
	Dim vUser
	Dim x
	Dim vGroup
	Dim y
	Dim vUserGroup
	Dim vMember
	
	vDomain = document.all.tiDomain.value

	vGroup = document.all.ddUsersOutputGroups.value
	
	vNum = 0

	Set objConnection = CreateObject("ADODB.Connection")
	Set objCommand = CreateObject("ADODB.Command")
	objConnection.Provider = "ADsDSOObject"
	objConnection.Open "Active Directory Provider"

	Set objCommand.ActiveConnection = objConnection
	objCommand.CommandText = "Select ADsPath from 'LDAP://" & vDomain & "' Where objectClass='group' And sAMAccountName='*" & vGroup & "*'"  
	objCommand.Properties("Page Size") = 5000
	objCommand.Properties("Searchscope") = 2
	On Error Resume Next
		Set objRecordSet = objCommand.Execute
	If Err.Number <> 0 Then
		jsUpdateLog("ERROR: Unable to connect to AD.")
		jsUpdateStatus("Complete")
		Err.Clear
		Exit Function
	End If
	
	If objRecordSet.RecordCount = 0 Then
		vData = vData & vGroup & " :: No matches" & vbCrLf
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			x = objRecordSet.Fields("ADsPath").Value
			objRecordSet.MoveNext
		Loop

		Set objUser = GetObject(x)

		objGroups = objUser.GetEx("member")
		For Each objGroup in objGroups
			vMember = Mid(objGroup,4,InStr(objGroup,",OU")-4)
			vMember = Replace(vMember,"\","")
			vData = vData & vGroup & " :: " & vMember & vbCrLf
			vNum = vNum + 1
		Next
		
		Set objUser = Nothing
	
	End If

	document.all.tiUsersResults.value = vData
	If vNum = 1 Then
		jsUpdateLog(vNum & " member found for group """ & vGroup & """")
	Else
		jsUpdateLog(vNum & " members found for group """ & vGroup & """")
	End If
	
	jsUpdateStatus("Complete")
	vbSleepy()
	
End Function

Function HexSIDToDec(strSID)
    ' Function to convert most hex SID values to decimal format.
    Dim arrbytSID, lngTemp, j
    ReDim arrbytSID(Len(strSID)/2 - 1)
    For j = 0 To UBound(arrbytSID)
        arrbytSID(j) = CInt("&H" & Mid(strSID, 2*j + 1, 2))
    Next
    If (UBound(arrbytSID) = 11) Then
        HexSIDToDec = "S-" & arrbytSID(0) & "-" & arrbytSID(1) & "-" & arrbytSID(8)
        Exit Function
    End If
    If (UBound(arrbytSID) = 15) Then
        HexSIDToDec = "S-" & arrbytSID(0) & "-" & arrbytSID(1) & "-" & arrbytSID(8)
        lngTemp = arrbytSID(15)
        lngTemp = lngTemp * 256 + arrbytSID(14)
        lngTemp = lngTemp * 256 + arrbytSID(13)
        lngTemp = lngTemp * 256 + arrbytSID(12)
        HexSIDToDec = HexSIDToDec & "-" & CStr(lngTemp)
        Exit Function
    End If
    HexSIDToDec = "S-" & arrbytSID(0) & "-" & arrbytSID(1) & "-" & arrbytSID(8)
    lngTemp = arrbytSID(15)
    lngTemp = lngTemp * 256 + arrbytSID(14)
    lngTemp = lngTemp * 256 + arrbytSID(13)
    lngTemp = lngTemp * 256 + arrbytSID(12)
    HexSIDToDec = HexSIDToDec & "-" & CStr(lngTemp)
    lngTemp = arrbytSID(19)
    lngTemp = lngTemp * 256 + arrbytSID(18)
    lngTemp = lngTemp * 256 + arrbytSID(17)
    lngTemp = lngTemp * 256 + arrbytSID(16)
    HexSIDToDec = HexSIDToDec & "-" & CStr(lngTemp)
    lngTemp = arrbytSID(23)
    lngTemp = lngTemp * 256 + arrbytSID(22)
    lngTemp = lngTemp * 256 + arrbytSID(21)
    lngTemp = lngTemp * 256 + arrbytSID(20)
    HexSIDToDec = HexSIDToDec & "-" & CStr(lngTemp)
    If (UBound(arrbytSID) > 23) Then
        lngTemp = arrbytSID(27)
        lngTemp = lngTemp * 256 + arrbytSID(26)
        lngTemp = lngTemp * 256 + arrbytSID(25)
        lngTemp = lngTemp * 256 + arrbytSID(24)
        HexSIDToDec = HexSIDToDec & "-" & CStr(lngTemp)
    End If
End Function

Function OctetToHexStr(ByVal arrbytOctet)
    ' Function to convert OctetString (byte array) to Hex string.
    Dim k
    OctetToHexStr = ""
    For k = 1 To Lenb(arrbytOctet)
        OctetToHexStr = OctetToHexStr & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
    Next
End Function

Function vbConnections()

	jsUpdateStatus("Please wait...")
	vbSleepy()

	Dim strUser, strComputer, strSID
	Dim vType
	vType = document.all.ddConnectionsWant.value
	
	strUser = document.all.tiConnectionsUser.value
	strComputer = document.all.tiConnectionsComputer.value
	
	If vbConnectionsGetUser(strUser) = "fail" Then
		document.all.tiConnectionsResults.value = strUser & " :: User not found"
		jsUpdateLog(strUser & " :: User not found")
		Exit Function
	End If
	If vbPing(strComputer) = "Ping failed" Then
		document.all.tiConnectionsResults.value = strComputer & " :: Computer not available"
		jsUpdateLog(strComputer & " :: Computer not found")
		Exit Function
	End If

	strSID = vbConnectionsGetUser(strUser)
	
	If vType = "Network Drives" Then
		document.all.tiConnectionsResults.value = ""
		vbFindNetworkDrives strComputer,strSID
	ElseIf vType = "Printers" Then
		document.all.tiConnectionsResults.value = ""
		vbFindPrinters strComputer,strSID
	End If
	
	jsUpdateStatus("Complete")
	vbSleepy()

End Function

Function vbConnectionsGetUser(funcvUser)
	
	Dim strFilter, vDomain, vUser, objRecordSet, objCommand, strQuery, strAttributes, strBase, objConnection
	vDomain = document.all.tiDomain.value
	vUser = funcvUser
	
	Set objConnection = CreateObject("ADODB.Connection")
	Set objCommand = CreateObject("ADODB.Command")
	objConnection.Provider = "ADsDSOObject"
	objConnection.Open "Active Directory Provider"

	Set objCommand.ActiveConnection = objConnection
	objCommand.CommandText = "Select objectSid from 'LDAP://" & vDomain & "' Where objectClass='user' And sAMAccountName='" & vUser & "'"
	objCommand.Properties("Page Size") = 5000
	objCommand.Properties("Searchscope") = 2
	On Error Resume Next
		Set objRecordSet = objCommand.Execute
	If Err.Number <> 0 Then
		jsUpdateLog("ERROR: Unable to connect to AD.")
		jsUpdateStatus("Complete")
		Err.Clear
		Exit Function
	End If
	
	If objRecordSet.RecordCount = 0 Then
		vbConnectionsGetUser = "fail"
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			vbConnectionsGetUser = HexSIDToDec(OctetToHexStr(objRecordSet.Fields("objectSid").Value))
			objRecordSet.MoveNext
		Loop
		
	End If
	
	objRecordSet.Close
	objConnection.Close

End Function

Function vbFindNetworkDrives(vComputer,vSID)

	Dim vData, vNum, vUN, vPW
	vData = ""
	
	vNum = 0
	
	If document.all.tiUsername.value <> "" Then
		vUN = document.all.tiUsername.value
	End If
	If document.all.tiPassword.value <> "" Then
		vPW = document.all.tiPassword.value
	End If

	Const HKR = &H80000000 'Root
	Const HKCU = &H80000001 'Current User
	Const HKLM = &H80000002 'Local Machine
	Const HKU = &H80000003 'Users

	Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
	On Error Resume Next
		If vUN = "" Or vPW = "" Then
			Set objSWbemService = objSWbemLocator.ConnectServer(vComputer, "root\default:StdRegProv")
		Else
			Set objSWbemService = objSWbemLocator.ConnectServer(vComputer, "root\default:StdRegProv", vUN, vPW)
		End If
	
		Set objReg = objSWbemService.Get("StdRegProv")
	If Err.Number <> 0 Then
		jsUpdateLog("ERROR: Unable to connect to """ & vComputer & """. Check permissions.")
		Exit Function
	End If
	
	strKeyPath = ""
	objReg.EnumKey HKU, strKeyPath, arrSubKeys

	For Each Subkey in arrSubKeys
		If Subkey = vSID Then
		
			strKeyPath2 = vSID & "\Network"
			objReg.EnumKey HKU,strKeyPath2, arrSubKeys2
			
			For Each Key in arrSubKeys2
			
				strKeyPath3 = vSID & "\Network\" & Key
				objReg.GetStringValue HKU, strKeyPath3, "RemotePath", strValue
				document.all.tiConnectionsResults.value = document.all.tiConnectionsResults.value & Key & " :: " & strValue & vbCrLf
				vNum = vNum + 1
				vbSleepy()
			
			Next
		
		End If
	Next
	
	'document.all.tiConnectionsResults.value = vData
	If vNum = 1 Then
		jsUpdateLog(vNum & " network drive found for """ & document.all.tiConnectionsUser.value & """ on """ & document.all.tiConnectionsComputer.value & """")
	Else
		jsUpdateLog(vNum & " network drives found for """ & document.all.tiConnectionsUser.value & """ on """ & document.all.tiConnectionsComputer.value & """")
	End If

End Function

Function vbListUsers()
	
	jsUpdateStatus("Please wait...")
	vbSleepy()
	
	vDomain = document.all.tiDomain.value
	vComputer = document.all.tiConnectionsComputer.value
	Dim vUser
	Dim vData
	Dim vSplitUsers
	Dim vNum
	Dim vCText
	Dim vUN
	Dim vPW
	Dim vCredentials
	
	If vbPing(vComputer) = "Ping success" Then
	
		If document.all.tiUsername.value <> "" Then
			vUN = document.all.tiUsername.value
		End If
		If document.all.tiPassword.value <> "" Then
			vPW = document.all.tiPassword.value
		End If
		
		Const HKR = &H80000000 'Root
		Const HKCU = &H80000001 'Current User
		Const HKLM = &H80000002 'Local Machine
		Const HKU = &H80000003 'Users

		Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
		
		On Error Resume Next
		
		If UCase(vComputer) = UCase(globHostname) Then
		
			Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & vComputer & "\root\default:StdRegProv")
			
		Else
		
			If vUN = "" Or vPW = "" Then
				Set objSWbemService = objSWbemLocator.ConnectServer(vComputer, "root\default:StdRegProv")
			Else
				Set objSWbemService = objSWbemLocator.ConnectServer(vComputer, "root\default:StdRegProv", vUN, vPW)
			End If
		
			Set objReg = objSWbemService.Get("StdRegProv")
			
		End If
		
		If Err.Number <> 0 Then
			jsUpdateLog("ERROR: Unable to connect to """ & vComputer & """. Check permissions.")
			Exit Function
		End If
		
		strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
		objReg.EnumKey HKLM, strKeyPath, arrSubKeys
		
		Set objConnection = CreateObject("ADODB.Connection")
		Set objCommand = CreateObject("ADODB.Command")
		objConnection.Provider = "ADsDSOObject"
		objConnection.Open "Active Directory Provider"
		Set objCommand.ActiveConnection = objConnection
		
		For Each Subkey in arrSubKeys
			
			If Len(Subkey) > 10 Then
				
				vCText = "Select sAMAccountName from 'LDAP://" & vDomain & "' Where objectClass='user' And objectSid='" & Subkey & "'"
				objCommand.CommandText = vCText

				objCommand.Properties("Page Size") = 5000
				objCommand.Properties("Searchscope") = 2
				On Error Resume Next
					Set objRecordSet = objCommand.Execute
				If Err.Number <> 0 Then
					jsUpdateLog("ERROR: Unable to connect to AD.")
					jsUpdateStatus("Complete")
					Err.Clear
					Exit Function
				End If
				
				If objRecordSet.RecordCount = 0 Then
					vUser = "fail"
				Else
					objRecordSet.MoveFirst
					Do Until objRecordSet.EOF
						vUser = objRecordSet.Fields("sAMAccountName").Value
						objRecordSet.MoveNext
					Loop
				End If
				
				If vUser <> "fail" Then
					vData = vData & vUser & vbCrLf
				End If
				
			End If
			
		Next
		
		objRecordSet.Close
		objConnection.Close
		
		jsTrim(vData)
		If globOS = 1 Then
			vSplitUsers = Split(vData,vbCrLf)
		Else
			vSplitUsers = Split(vData,Chr(10))
		End If
		
		If UBound(vSplitUsers) > 0 Then
			
			jsRemoveDD("ddConnectionsUser")
			jsAddDDItem "--Select User--","ddConnectionsUser"
		
			For Each User in vSplitUsers
			
				If User <> "" Then
					jsAddDDItem User,"ddConnectionsUser"
					vNum = vNum + 1
				End If
			
			Next
			
			If vNum = 1 Then
				jsUpdateLog(vNum & " user found on """ & vComputer & """")
			Else
				jsUpdateLog(vNum & " users found on """ & vComputer & """")
			End If
		
		Else
		
			jsUpdateLog("No users found on """ & vComputer & """")
		
		End If
	
	Else
	
		jsUpdateLog("ERROR: Unable to connect to """ & vComputer & """. Ping failed.")
		vbSleepy()
	
	End If
	
	jsUpdateStatus("Complete")
	vbSleepy()
	
End Function

Function vbFindPrinters(vComputer,vSID)

	Dim vData, vNum, vUN, vPW
	Dim vSplitKey
	
	vData = ""
	vNum = 0
	
	If document.all.tiUsername.value <> "" Then
		vUN = document.all.tiUsername.value
	End If
	If document.all.tiPassword.value <> "" Then
		vPW = document.all.tiPassword.value
	End If

	Const HKR = &H80000000 'Root
	Const HKCU = &H80000001 'Current User
	Const HKLM = &H80000002 'Local Machine
	Const HKU = &H80000003 'Users

	Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
	
	If UCase(vComputer) = UCase(globHostname) Then
	
		Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & vComputer & "\root\default:StdRegProv")
		
	Else
	
		On Error Resume Next
			If vUN = "" Or vPW = "" Then
				Set objSWbemService = objSWbemLocator.ConnectServer(vComputer, "root\default:StdRegProv")
			Else
				Set objSWbemService = objSWbemLocator.ConnectServer(vComputer, "root\default:StdRegProv", vUN, vPW)
			End If
		
			Set objReg = objSWbemService.Get("StdRegProv")
			
		If Err.Number <> 0 Then
			jsUpdateLog("ERROR: Unable to connect to """ & vComputer & """. Check permissions.")
			Exit Function
		End If
		
	End If
	
	strKeyPath = ""
	objReg.EnumKey HKU, strKeyPath, arrSubKeys

	For Each Subkey in arrSubKeys
		If Subkey = vSID Then
		
			strKeyPath2 = vSID & "\Printers\Connections\"
			objReg.EnumKey HKU,strKeyPath2, arrSubKeys2
			
			If UBound(arrSubKeys2) >= 0 Then
			
				For Each Key in arrSubKeys2
					
					vSplitKey = Split(Key,",")
					document.all.tiConnectionsResults.value = document.all.tiConnectionsResults.value & "\\" & vSplitKey(2) & "\" & vSplitKey(3) & vbCrLf
					vNum = vNum + 1
					vbSleepy()
				
				Next
				
			End If
		
		End If
		
	Next
	
	'document.all.tiConnectionsResults.value = vData
	
	If vNum = 1 Then
		jsUpdateLog(vNum & " printer found for """ & document.all.tiConnectionsUser.value & """ on """ & document.all.tiConnectionsComputer.value & """")
	Else
		jsUpdateLog(vNum & " printers found for """ & document.all.tiConnectionsUser.value & """ on """ & document.all.tiConnectionsComputer.value & """")
	End If

End Function

Function vbGetApps()
	
	jsUpdateStatus("Please wait...")
	vbSleepy()
	
    Dim strComputer
    Dim strKey
    Dim strEntry1a
    Dim strEntry1b
    Dim strEntry1c
    Dim strSubKey
    Dim strValue1
    Dim strValue2
    Dim strValue3
    Dim objReg
    Dim arrSubKeys
    Dim i
    Dim strAppName
    Dim strAppList
    Dim strSplitList
    Dim strFinAppList
	Dim vInitial
	Dim vInitial2
	Dim vAllAssets
	Dim x
	Dim vAsset
	Dim vOutput
	Dim objFSO, objFile, oShell, vAppData, vOutFile
	Dim strAppList2
	Dim vInspected, vExtracted
	Dim vOpenApps
	Dim vXL
	Dim vUN, vPW
	
	If document.all.tiUsername.value <> "" Then
		vUN = document.all.tiUsername.value
	End If
	If document.all.tiPassword.value <> "" Then
		vPW = document.all.tiPassword.value
	End If

	vInitial = document.all.tiComputersOutput.value
	document.all.tiComputersOutput.value = vInitial
	vInitial2 = jsTrim(vInitial)
	
	If globOS = 1 Then
		vAllAssets = Split(vInitial2,vbCrLf)
	Else
		vAllAssets = Split(vInitial2,Chr(10))
	End If
	
	vExtracted = 0
	
	For x = 0 To UBound(vAllAssets)
	
		If vAllAssets(x) <> "" Then
		
			vInspected = vInspected + 1
			
			If instr(vAllAssets(x)," :: ") > 0 Then
			
				strComputer = right(vAllAssets(x),len(vAllAssets(x)) - instr(vAllAssets(x)," :: ") - 3)
			
			Else
			
				strComputer = vAllAssets(x)
				
			End If
			
			If strComputer = "No matches" Then
			
				If document.all.ckComputersNoMatch.checked = True Then
				
					strComputer = "skip"
				
				Else
				
					strComputer = left(vAllAssets(x),instr(vAllAssets(x)," :: ")-1)
				
				End If
			
			End If
			
			If strComputer = "skip" Then
			
				vInspected = vInspected - 1
				
			Else
			
				If vbPing(strComputer) = "Ping success" Then
				
					vExtracted = vExtracted + 1
					
					Const HKR = &H80000000 'Root
					Const HKCU = &H80000001 'Current User
					Const HKLM = &H80000002 'Local Machine
					Const HKU = &H80000003 'Users
					
					strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
					strEntry1a = "DisplayName"
					strEntry1b = "QuietDisplayName"
					strEntry1c = "NoDisplayName"

					Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
	
					If UCase(strComputer) = UCase(globHostname) Then
					
						Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
						
					Else
					
						On Error Resume Next
							If vUN = "" Or vPW = "" Then
								Set objSWbemService = objSWbemLocator.ConnectServer(strComputer, "root\default:StdRegProv")
							Else
								Set objSWbemService = objSWbemLocator.ConnectServer(strComputer, "root\default:StdRegProv", vUN, vPW)
							End If
						
							Set objReg = objSWbemService.Get("StdRegProv")
							
						If Err.Number <> 0 Then
							jsUpdateLog("ERROR: Unable to connect to """ & strComputer & """. Check permissions.")
							Exit Function
						End If
						
					End If
					
					objReg.EnumKey HKLM, strKey, arrSubKeys
					
					For Each strSubKey In arrSubKeys

						objReg.GetStringValue &H80000002, strKey & strSubKey, strEntry1a, strValue1
						
						strAppName = ""
						
						If strValue1 <> "" Then
							strAppName = vbNonAscii(strValue1)
						End If
						
						If strAppName <> "" Then
							strAppList2 = strAppList2 & strAppName & "  ;  "
						End If

					Next
					
					strAppList = trim(strAppList2)
					
					strSplitList = Split(strAppList, "  ;  ")

					For i = 0 To UBound(strSplitList)

						vOutput = vOutput + strComputer & "," & strSplitList(i) & vbCrLf

					Next
					
					strFinAppList = ""
					strAppList = ""
					strAppName = ""
					i = 0
				
					Set objReg = Nothing
					
					jsUpdateLog("Apps extracted from " & strComputer)
				
				Else
				
					vOutput = vOutput + strComputer & ",Unavailable" & vbCrLf
					jsUpdateLog("Unable to connect to " & strComputer)
					
				End If
				
			End If
		
		End If
		
	Next
	
	If vOutput <> "" Then
		
		vOutput = "Computer,Application" & vbCrLf & vOutput
		
		Const ssfAPPDATA = &H1A
		Set oShell = CreateObject("Shell.Application")
		vAppData = oShell.NameSpace(ssfAPPDATA).Self.Path
	
		vOutFile = vAppData & "\AppsOutput.csv"
		vbCreateFile vOutFile,vOutput
		
		jsUpdateLog("Apps results - " & vInspected & " inspected, " & vExtracted & " extracted")
		
		vOpenApps = MsgBox("Do you want to open the list of extracted apps now?",vbYesNo)
		If vOpenApps = 6 Then
		
			vbOpenExcel(vOutFile)
		
		End If
		
	End If
	
	Set objReg = Nothing
	
	jsUpdateStatus("Complete")
	vbSleepy()
	
End Function

Function vbOpenExcel(vFile)

	Dim xl

	Set xl = CreateObject("Excel.application")

	xl.Application.Workbooks.Open vFile
	xl.Application.Visible = True

	Set xl = Nothing

End Function

Function vbNonAscii(vInput)

	Set objRegExp = New RegExp
	objRegExp.Global = True
	objRegExp.IgnoreCase = True
	objRegExp.Pattern = "[^\x20-\x7E]"
	vbNonAscii = objRegExp.Replace(vInput,"")

End Function

Function vbCreateFile(vLoc,vContent)

	Dim objFSO
	Dim objFile
	
	Const ForReading = 1
	Const ForWriting = 2

	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.CreateTextFile(vLoc, True)
	objFile.Close

	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(vLoc, ForWriting)
	
	'objFile.WriteLine vContent
	objFile.Write vContent
	objFile.Close
	
	Set objFile = Nothing
	Set ObjFSO = Nothing

End Function

Function vbProfileDetails()

	Dim vUser, vSID, vSize, vProfile, vUN, vPW
	Dim vComputer, vMessage
	
	jsUpdateStatus("Please wait...")
	vbSleepy()
	
	vComputer = document.all.tiConnectionsComputer.value
	vUser = document.all.tiConnectionsUser.value
	
	document.getElementById("divConnectionsProfileLocation").innerHTML = ""
	document.getElementById("divConnectionsProfileSize").innerHTML = ""
	
	If document.all.tiUsername.value <> "" Then
		vUN = document.all.tiUsername.value
	End If
	If document.all.tiPassword.value <> "" Then
		vPW = document.all.tiPassword.value
	End If

	Const HKR = &H80000000 'Root
	Const HKCU = &H80000001 'Current User
	Const HKLM = &H80000002 'Local Machine
	Const HKU = &H80000003 'Users
	
	If vbPing(vComputer) = "Ping success" Then
	
		vSID = vbConnectionsGetUser(vUser)
		
		If vSID <> "fail" Then

			Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
			
			If UCase(vComputer) = UCase(globHostname) Then
			
				Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & vComputer & "\root\default:StdRegProv")
				
			Else
			
				On Error Resume Next
					If vUN = "" Or vPW = "" Then
						Set objSWbemService = objSWbemLocator.ConnectServer(vComputer, "root\default:StdRegProv")
					Else
						Set objSWbemService = objSWbemLocator.ConnectServer(vComputer, "root\default:StdRegProv", vUN, vPW)
					End If
				
					Set objReg = objSWbemService.Get("StdRegProv")
				If Err.Number <> 0 Then
					jsUpdateLog("ERROR: Unable to connect to """ & vComputer & """. Check permissions.")
					Exit Function
				End If
				
			End If
			
			strListProfiles = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\"
			objReg.EnumKey HKLM, strListProfiles, arrListProfiles

			For Each subListProfile in arrListProfiles

				If subListProfile = vSID Then
					
					strSingleProfile = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & vSID & "\"
					objReg.GetExpandedStringValue HKLM,strSingleProfile,"ProfileImagePath",vProfile
					
					If Left(vProfile,13) = "%SystemDrive%" Then
						vProfile = Replace(vProfile,"%SystemDrive%","C:","\\" & vComputer & "\C$")
					Else
						vDriveLetter = Left(vProfile,1)
						vProfile = Replace(vProfile,vDriveLetter & ":","\\" & vComputer & "\" & vDriveLetter & "$")
					End If
					
					document.getElementById("divConnectionsProfileLocation").innerHTML = "<b>Profile location:</b> <a href=""#"" onclick=""vbGoToProfile('" & Replace(vProfile,"\","\\") & "')"">" & vProfile & "</a>"
					vbSleepy()
					
					Dim FSO, objFolder, datafolder, metric
					Set FSO = CreateObject("Scripting.FileSystemObject")
					
					If FSO.FolderExists(vProfile) Then
					
						On Error Resume Next
					
							Set objFolder = FSO.GetFolder(vProfile)

							If objFolder.Size >= 1073741824 Then
								datafolder = objFolder.Size/1073741824
								metric = "GB"
							Else
								datafolder = objFolder.Size/1048576
								metric = "MB"
							End If
							
							document.getElementById("divConnectionsProfileSize").innerHTML = "<b>Profile size:</b> " & FormatNumber(datafolder,2) & " " & metric
							vMessage = "Profile size found"
							
						If Err.Number <> 0 Then
							vMessage = "Unable to gauge size of user's profile. Check permissions."
						End If
					Else
						vMessage = "Unable to connect to user's profile"
					End If
				
				End If
				
			Next
			
		End If
	
	End If
	
	jsUpdateLog(vMessage)
	
	jsUpdateStatus("Complete")
	vbSleepy()

End Function

Function vbGoToProfile(vLocation)
	
	Set objShell = CreateObject("Wscript.Shell")
	
	strPath = "explorer.exe /e," & vLocation
	objShell.Run strPath

End Function

</script>

<script language="javascript" type="text/javascript">
function jsConnectionsUser()
{
	if ((document.getElementById("ddConnectionsUser") != "--Select User--") && (document.getElementById("ddCOnnectionsUsers") != ""))
	{
		document.getElementById("tiConnectionsUser").value = document.getElementById("ddConnectionsUser").value;
	}
}
function jsAddDDItem(vItem,vDD)
{
	var select = document.getElementById(vDD);

	var opt = document.createElement('option');
	opt.value = vItem;
	opt.innerHTML = vItem;
	select.appendChild(opt);
}

function jsRemoveDD(vDD)
{
	var vLen;
	var vSel;
	var i = 0;
	vSel = document.getElementById(vDD)
	vLen = vSel.options.length;
	while (i<vLen)
	{
		vSel.remove(0);
		i++
	}
}

function jsSplit()
{
	var vInit = document.getElementById("tiUsersInput").value;
	var vSplit = vInit.split("\n");
	for (var i = 0; i < vSplit.length; i++) 
	{
		alert(vSplit[i]);
	}
}

function jsType(vType)
{
	if (vType == "users")
	{
		document.getElementById("divComputers").style.display = "none";
		document.getElementById("divHeaderComputers").style.borderBottom = "";
		document.getElementById("divHeaderComputers").style.fontWeight = "normal";
		document.getElementById("divConnections").style.display = "none";
		document.getElementById("divHeaderConnections").style.borderBottom = "";
		document.getElementById("divHeaderConnections").style.fontWeight = "normal";
		
		document.getElementById("divUsers").style.display = "";
		document.getElementById("divHeaderUsers").style.borderBottom = "solid 2px #529AEA";
		document.getElementById("divHeaderUsers").style.fontWeight = "bold";
	}
	if (vType == "computers")
	{
		document.getElementById("divUsers").style.display = "none";
		document.getElementById("divHeaderUsers").style.borderBottom = "";
		document.getElementById("divHeaderUsers").style.fontWeight = "normal";
		document.getElementById("divConnections").style.display = "none";
		document.getElementById("divHeaderConnections").style.borderBottom = "";
		document.getElementById("divHeaderConnections").style.fontWeight = "normal";
		
		document.getElementById("divComputers").style.display = "";
		document.getElementById("divHeaderComputers").style.borderBottom = "solid 2px #529AEA";
		document.getElementById("divHeaderComputers").style.fontWeight = "bold";
	}
	if (vType == "connections")
	{
		document.getElementById("divUsers").style.display = "none";
		document.getElementById("divHeaderUsers").style.borderBottom = "";
		document.getElementById("divHeaderUsers").style.fontWeight = "normal";
		document.getElementById("divComputers").style.display = "none";
		document.getElementById("divHeaderComputers").style.borderBottom = "";
		document.getElementById("divHeaderComputers").style.fontWeight = "normal";
		
		document.getElementById("divConnections").style.display = "";
		document.getElementById("divHeaderConnections").style.borderBottom = "solid 2px #529AEA";
		document.getElementById("divHeaderConnections").style.fontWeight = "bold";
	}
}

function jsScriptRun(vScript)
{
	if (vScript == "exit")
	{
		window.close();
	}
	if (vScript == "get_computers")
	{
		if (document.getElementById("tiComputersInput").value == "")
		{
			jsUpdateLog("ERROR: Please ensure computers are entered");
			document.getElementById("tiComputersInput").focus();
			return;
		}
		if ((document.getElementById("ddComputersInputWant").value == "I Want") || (document.getElementById("ddComputersInputWant").value == "Full Asset Number and..."))
		{
			jsUpdateLog("EEROR: Please choose an item to report on");
			document.getElementById("ddComputersInputWant").focus();
			return;
		}
		else
		{
			vbGetComputers();
		}
	}
	if (vScript == "get_users")
	{
		if (document.getElementById("tiUsersInput").value == "")
		{
			jsUpdateLog("ERROR: Please ensure users details are entered");
			document.getElementById("tiUsersInput").focus();
			return;
		}
		if ((document.getElementById("ddUsersInputWant").value == "I Want") || (document.getElementById("ddUsersInputWant").value == "Account Name and...") || (document.getElementById("ddUsersInputHave").value == "I Have"))
		{
			jsUpdateLog("ERROR: Please choose items to verify against and to report on")
			return;
		}
		if (document.getElementById("ddUsersInputWant").value == "Group Membership")
		{
			document.getElementById("tiUsersOutput").value = "";
			vbGetGroups();
		}
		else
		{
			document.getElementById("tiUsersOutput").value = "";
			vbGetUsers();
		}
	}
	if (vScript == "get_ip")
	{
		if (document.getElementById("tiComputersOutput").value == "")
		{
			jsUpdateLog("ERROR: No computers to find and IP for");
			return;
		}
		else
		{
			document.getElementById("tiComputersResults").value = "";
			vbGetIP();
		}
	}
	if (vScript == 'ping')
	{
		if (document.getElementById("tiComputersOutput").value == "")
		{
			jsUpdateLog("ERROR: No computers to ping");
			return;
		}
		else
		{
			document.getElementById("tiComputersResults").value = "";
			vbPingComputers();
		}
	}
	if (vScript == "group_members")
	{
		if (document.getElementById("ddUsersOutputGroups").value == "")
		{
			jsUpdateLog("ERROR: Please ensure a group is selected");
			return;
		}
		else
		{
			vbGetMembers();
		}
	}
	if (vScript == "connections")
	{
		if ((document.getElementById("tiConnectionsUser").value == "") || (document.getElementById("tiConnectionsComputer").value == ""))
		{
			jsUpdateLog("ERROR: Please ensure details are entered");
			return;
		}
		else if (document.getElementById("ddConnectionsWant").value == "I Want")
		{
			jsUpdateLog("ERROR: Please choose want to report on");
			document.getElementById("ddConnectionsWant").focus();
			return;
		}
		else
		{
			vbConnections();
		}
	}
	if (vScript == "list_users")
	{
		if (document.getElementById("tiConnectionsComputer").value == "")
		{
			jsUpdateLog("ERROR: Please enter a computer name");
			return;
		}
		else
		{
			vbListUsers();
		}
	}
	if (vScript == 'apps')
	{
		if (document.getElementById("tiComputersOutput").value == "")
		{
			jsUpdateLog("ERROR: No assets to generate apps for");
			return;
		}
		else
		{
			vbGetApps();
		}
	}
	if (vScript == "profile_details")
	{
		if (document.getElementById("tiConnectionsUser").value == "")
		{
			jsUpdateLog("ERROR: Please enter a username");
			return;
		}
		if (document.getElementById("tiConnectionsComputer").value == "")
		{
			jsUpdateLog("ERROR: Please enter a computer name");
			return;
		}
		else
		{
			vbProfileDetails();
		}
	}
}

function jsReset(vType)
{
	var vMess;
	if (vType == "computers")
	{
		vMess = confirm("Are you sure you want to reset?")
		if (vMess)
		{
			document.getElementById("tiComputersInput").value = "";
			document.getElementById("tiComputersOutput").value = "";
			document.getElementById("tiComputersResults").value = "";
			document.getElementById("ddComputersInputWant").value = "I Want";
		}
	}
	if (vType == "users")
	{
		vMess = confirm("Are you sure you want to reset?")
		if (vMess)
		{
			document.getElementById("tiUsersInput").value = "";
			document.getElementById("tiUsersOutput").value = "";
			document.getElementById("tiUsersResults").value = "";
			document.getElementById("ddUsersInputHave").value = "I Have";
			document.getElementById("ddUsersInputWant").value = "I Want";
			jsRemoveDD("ddUsersOutputGroups");
		}
	}
	if (vType == "connections")
	{
		vMess = confirm("Are you sure you want to reset?")
		if (vMess)
		{
			document.getElementById("tiConnectionsComputer").value = "";
			document.getElementById("tiConnectionsUser").value = "";
			document.getElementById("tiConnectionsResults").value = "";
			document.getElementById("ddConnectionsWant").value = "I Have";
			jsRemoveDD("ddConnectionsUser");
			document.getElementById("divConnectionsProfileLocation").innerHTML = "";
			document.getElementById("divConnectionsProfileSize").innerHTML = "";
		}
	}
}

function jsFullReset()
{
	var vMess = confirm("Are you sure you want to reset?")
	if (vMess)
	{
		document.getElementById("tiComputersInput").value = "";
		document.getElementById("tiComputersOutput").value = "";
		document.getElementById("tiComputersResults").value = "";
		document.getElementById("ddComputersInputWant").value = "I Want";
		
		document.getElementById("tiUsersInput").value = "";
		document.getElementById("tiUsersOutput").value = "";
		document.getElementById("tiUsersResults").value = "";
		document.getElementById("ddUsersInputHave").value = "I Have";
		document.getElementById("ddUsersInputWant").value = "I Want";
		jsRemoveDD("ddUsersOutputGroups");
		
		document.getElementById("tiConnectionsComputer").value = "";
		document.getElementById("tiConnectionsUser").value = "";
		document.getElementById("tiConnectionsResults").value = "";
		document.getElementById("ddConnectionsWant").value = "I Have";
		jsRemoveDD("ddConnectionsUser");
		document.getElementById("divConnectionsProfileLocation").innerHTML = "";
		document.getElementById("divConnectionsProfileSize").innerHTML = "";
		
		jsClearLog();
		jsType("computers");
	}
}

function jsClearLog()
{
	document.getElementById("tiLog").value = "";
}

function jsUpdateLog(vMessage)
{
	var vOrig;
	if (document.getElementById("tiLog").value == "")
	{
		document.getElementById("tiLog").value = vMessage;
	}
	else
	{
		vOrig = document.getElementById("tiLog").value;
		document.getElementById("tiLog").value = vMessage + "\n" + vOrig;
	}
}

function jsTrim(vString)
{
	return vString.replace(/^\s+|\s+$/g,"");
}

function jsUpdateStatus(vMessage)
{
	document.getElementById("divStatus").innerHTML = vMessage;
}

</script>